//
// Created by 崔世强 on 2018/3/25.
// Copyright (c) 2018 Facebook. All rights reserved.
//

import Foundation
import WCDBSwift

class MessageDao {
    
    class func insert(_ msg: Message) throws {
        let db = SQLiteHelper.getDb()
        try db.insert(msg, intoTable: Message.table)
    }
    
    class func getLastMsg(sessionServerId:Int, chatType: ChatType) throws -> Message?  {
        let db = SQLiteHelper.getDb()
        let lastMsg:Message? = try db.getObject(on: Message.CodingKeys.all, fromTable: Message.table, where: Message.CodingKeys.sessionServerId == sessionServerId && Message.CodingKeys.chatType == chatType,
                                                orderBy: [Message.CodingKeys.createTime.order(.descending)])
        return lastMsg
    }
    
    class func getUnreadCount(sessionServerId:Int, chatType:ChatType) throws -> Int {
        var unreadCount:Int = 0
        let db = SQLiteHelper.getDb()
        let unread = try db.getValue(on: Message.CodingKeys.any.count(), fromTable: Message.table,
                                     where: Message.CodingKeys.sessionServerId == sessionServerId  && Message.CodingKeys.unread == true && Message.CodingKeys.deleted == false && Message.CodingKeys.chatType == chatType)
        unreadCount = Int(unread.int32Value)
        return unreadCount
    }
    
    class func getUnreadCount(chatType:ChatType) throws -> Int {
        var unreadCount:Int = 0
        let db = SQLiteHelper.getDb()
        let unread = try db.getValue(on: Message.CodingKeys.any.count(), fromTable: Message.table, where: Message.CodingKeys.unread == true && Message.CodingKeys.deleted == false && Message.CodingKeys.chatType == chatType)
        unreadCount = Int(unread.int32Value)
        return unreadCount
    }
    
    class func getUnreadCount() throws -> Int {
        var unreadCount:Int = 0
        let db = SQLiteHelper.getDb()
        let unread = try db.getValue(on: Message.CodingKeys.any.count(), fromTable: Message.table, where: Message.CodingKeys.unread == true && Message.CodingKeys.deleted == false)
        unreadCount = Int(unread.int32Value)
        return unreadCount
    }
    
    class func getMsg(id: Int) throws -> Message?{
        let db = SQLiteHelper.getDb()
        let msg:Message? = try db.getObject(on: Message.CodingKeys.all, fromTable: Message.table, where: Message.CodingKeys.id == id)
        return msg
    }
    
    class func delete(sessionId: Int, chatType: ChatType) throws {
        let db = SQLiteHelper.getDb()
        try db.delete(fromTable: Message.table, where: Message.CodingKeys.chatType == chatType.rawValue && Message.CodingKeys.sessionServerId == sessionId)
    }
    
    
    class func deleteMsg(msgId: Int, chatType: ChatType) throws {
        let db = SQLiteHelper.getDb()
        try db.delete(fromTable: Message.table, where: Message.CodingKeys.chatType == chatType.rawValue && Message.CodingKeys.id == msgId)
    }

    class func logicDelete(msg:Message) throws {
        let db = SQLiteHelper.getDb()
        msg.deleted = true
        try db.update(table: Message.table, on: Message.CodingKeys.deleted, with: msg, where: Message.CodingKeys.id == msg.id)
    }
    
    class func updateMsgReadState(msg:Message) throws{
        let db = SQLiteHelper.getDb()
        try db.update(table: Message.table, on: Message.CodingKeys.audioRead, with: msg, where: Message.CodingKeys.id == msg.id)
    }
    
    class func updateMsgState(msg:Message) throws{
        let db = SQLiteHelper.getDb()
        try db.update(table: Message.table, on: Message.CodingKeys.status, with: msg, where: Message.CodingKeys.id == msg.id)
    }
}
